package com.ayoka.mvp;

import com.ayoka.base.BasePresenter;
import com.ayoka.base.IBaseView;
import com.ayoka.bean.BaseResponse;
import com.ayoka.bean.RequestBase;
import com.ayoka.http.API;
import com.ayoka.http.OkHttpListener;
import com.ayoka.mvp.CourseEstimateQuestionMVP.CourseEstimateQuestionModel.CourseEstimateQuestionMultiEntity;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimateModel.CourseEstimateQuestionBean;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimateModel.QuestionOption;
import com.jcp.baselibrary.brvah.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gabriel on 2017/3/5.
 */

public interface CourseEstimateQuestionMVP {

    interface CourseEstimateQuestionModel {

        class CourseEstimateQuestionMultiEntity extends MultiItemEntity {
            public static final int QUESTION = 1;// 问题
            public static final int OPTION = 2;// 选项
            public static final int BUTTON = 3;// 按钮

            public CourseEstimateQuestionMultiEntity(int itemType, Object object) {
                super(itemType, object);
            }
        }

        class RequestCourseEstimateAnswerBean extends RequestBase {
            public long QuestionID;
            public long FactorGroupID;
            public long TestingRecordID;
            public int QuestionIndex;
            public int Result;
        }
    }

    interface ICourseEstimateQuestionView extends IBaseView {

        void configCourseEstimateQuestionMultiEntityData(List<CourseEstimateQuestionMultiEntity> formatList);

        void onSubmitCourseEstimateAnswerCallback(boolean isSuccess);
    }

    class CourseEstimateQuestionPresenter extends BasePresenter<ICourseEstimateQuestionView> {



        public void formatList(boolean isFirst, CourseEstimateQuestionBean questionBean) {
            List<CourseEstimateQuestionMultiEntity> formatList = new ArrayList<>();
            if (questionBean != null) {
                formatList.add(new CourseEstimateQuestionMultiEntity(CourseEstimateQuestionMultiEntity.QUESTION, questionBean.Question + "\n" + questionBean._Options));
                if (questionBean.Options != null && !questionBean.Options.isEmpty()) {
                    for (QuestionOption option : questionBean.Options) {
                        formatList.add(new CourseEstimateQuestionMultiEntity(CourseEstimateQuestionMultiEntity.OPTION, option));
                    }
                }
                if (!isFirst) {
                    formatList.add(new CourseEstimateQuestionMultiEntity(CourseEstimateQuestionMultiEntity.BUTTON, null));
                }
            }
            mViewRef.get().configCourseEstimateQuestionMultiEntityData(formatList);
        }

        public void submitCourseEstimateAnswer(long courseEstimateId, long factorGroupID, long recordId, int questionIndex, int score) {
            API.submitCourseEstimateAnswer(this, courseEstimateId, factorGroupID, recordId, questionIndex, score, new OkHttpListener<BaseResponse>() {
                @Override
                public void onSuccess(BaseResponse response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().onSubmitCourseEstimateAnswerCallback(true);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                    mViewRef.get().onSubmitCourseEstimateAnswerCallback(false);
                }
            });
        }
    }

}
